import { ref,computed } from "vue";
import { defineStore } from "pinia";

//选项式
export const useStoreOne = defineStore('counterone',{
  state:()=>({
      count:0,
      name:'Bob',
      isAdmin:true,
      items:[] as string[],
      hasChanged:true,
  }),
  // state:()=>{
  //   return {
  //     count:0,
  //     name:'Bob',
  //     isAdmin:true,
  //     items:[] as string[],
  //     hasChanged:true,
  //   }
  // }
  getters: {
    doubleCount:(state)=>state.count * 2,
    doublePlusone:(state)=>state.count * 2 + 1,
    // doublePlusone():number{
    //   return this.doubleCount + 1
    // }
  },
  actions: {
    increment(){
      this.count++;
    },
    //$reset重置演示
    reset(){
      this.$reset();
    },
  },
  //自定义配置数据持久化
  persist: {
    key:'user',
    storage:localStorage,
    pick:['count'],
    serializer: {
      serialize: (value:unknown) => btoa(JSON.stringify(value)), //base64编码
      deserialize: (value: string) => JSON.parse(atob(value)), //base64解码
    },
  },
});
//组合式
export const useStoreTwo=defineStore('countertwo',()=>{
  const count=ref(0)
  const name=ref('Bob')
  const isAdmin=ref(true)
  const doubleCount=computed(()=>count.value * 2)
  const doublePlusone=computed(()=>{
    return doubleCount.value + 1
  })
  const increment=()=>{
    count.value++
  }
  const reset=()=>{
    count.value=0,
    name.value='Bob',
    isAdmin.value=true
  }
  return {count,name,isAdmin,doubleCount,doublePlusone,increment,reset}
},
{
  persist:{
    key:'users',
    storage:localStorage,
    pick:['name'],
    serializer: {
      serialize: (value:unknown) => btoa(JSON.stringify(value)), //base64编码
      deserialize: (value: string) => JSON.parse(atob(value)), //base64解码
    },
  }
}
)
